<?php

// [ 总控器 ]

namespace app\mobile\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Cookie;
use app\common\logic\Login as LoginLogic;

class Base extends Controller
{
    public function _initialize()
    {
        // p(Session::get('wechat'));
        // p(Session::get('member'));
        // die;

        // ----------------------------------------------------------------
        // [ 调试模式 ]
        if( isset($_GET['debug']) ) {
            $wechat = array(
                'unionid'    => 'okpjT0qwLBr1L41ZLesDFUqhJcf4',
                'openid'     => 'ocjMZw-OgxAmuGQhn00PzbWwsSIE',
                'nickname'   => '明日东华｜曾凡东',
                'headimgurl' => 'http://wx.qlogo.cn/mmopen/IcThv8C3Tp3icWOia880gXKsy2XmR56NYkMqEJ8zfptxibWRFQYhjcUsImib0Y33s9A5vaVesf8Y8fsmlRFYzcDcZYMZmHFnkVMG/0',
                'sex'        => 1,
                'country'    => '中国',
                'province'   => '四川',
                'city'       => '成都'
            );
            $member = Db::name('member')->where('unionid',$wechat['unionid'])->find();
            $member['info'] = Db::name('member_info')->where('member_id',$member['id'])->find();
			// 写入Session
			Session::set('wechat',$wechat);
            // 登录处理
            LoginLogic::run($member);
        }
        if( isset($_GET['debug2']) ) {
            $wechat = array(
                'unionid'    => 'okpjT0uVhqRrigU00Fl693Le4aiM',
                'openid'     => 'oM8Jwv9LRS6dsLHGcilTOrY49uwA',
                'nickname'   => '18123221961',
                'headimgurl' => '/public/temp/member_headimgurl_default.png',
                'sex'        => 1,
                'country'    => '中国',
                'province'   => '四川',
                'city'       => '成都'
            );
            $member = Db::name('member')->where('unionid',$wechat['unionid'])->find();
            $member['info'] = Db::name('member_info')->where('member_id',$member['id'])->find();
			// 写入Session
			Session::set('wechat',$wechat);
            // 登录处理
            LoginLogic::run($member);
        }
        // ----------------------------------------------------------------

        // 上线删除此部分----------------------------------------------------------------------------
        restart:
        // 上线删除此部分----------------------------------------------------------------------------


        // 微信授权登录
        if ( !Session::has('wechat') ) {
            Session::set( 'callbackUri', $this->request->url(true) );
            $this->redirect('@wechat/auth');
        }

        // p(Session::get('wechat'));die;



        // 完善用户资料
        if ( !Session::get('member') ) {
            $unionid = Session::get('wechat.unionid');
            $member = Db::name('member')->where('unionid',$unionid)->field('password',true)->find();

            if ( !is_null($member) ) {
                // 如果缺省openid,则完善之
                if ( empty($member['openid']) ) {
                    $openid = Session::get('wechat.openid');
                    Db::name('member')->where('id',$member['id'])->update(['openid'=>$openid]);
                }
                // 帐号被锁定
                if ( 1 == $member['status'] ) die('您的账号被限制使用，如有疑问，请联系管理员！');
            } else {
                // 第一次去完善信息
                $this->redirect('@mobile/complete');
            }

            $member['info'] = Db::name('member_info')->where('member_id',$member['id'])->find();

            // 登录处理
            LoginLogic::run($member);
        }
        // 上线删除此部分----------------------------------------------------------------------------
        else {
            $unionid = Session::get('wechat.unionid');
            $member = Db::name('member')->where('unionid',$unionid)->field('password',true)->find();
            if ( is_null($member) ) {
                Session::delete('wechat');
                Session::delete('member');
                goto restart;
            }
        }
        // 上线删除此部分----------------------------------------------------------------------------
    }
}
